<html>
<head>
    <script type="text/javascript" src="http://10.147.18.146:9001/web-apps/apps/api/documents/api.js"></script>
</head>
<body>
<div id="placeholder"></div>
</body>
<script src="../js/jquery-3.5.0.min.js" type="text/javascript" charset="utf-8"></script>
<script>
    let url= window.location.href;
    let http= url.match(/^h.+:\d+/)


    var onlyOffice = {
        //文件下载地址
        documentUrl: http + "/system/file/download?fileId=",
        //文件回调地址
        callbackUrl: http + "/system/file/saveModify",
        //文件信息
        fileInfo:{},
        //onlyOffice基本配置
        config: {
            "document": {
                "fileType": "docx",
                "key": "",
                "title": "",
                "url": "",
            },
            "documentType": "text",
            "editorConfig": {
                "callbackUrl": this.callbackUrl,
                "lang": "zh-CN",
                "mode": "view",
                "user": {
                    "id": "78e1e841"
                }
            }
        },
        //初始化
        init:function () {
            var id = this.getQueryString("fileId");
            if(!id){
                this.fileInfo.id = "3ba16c01-f98d-4b33-96fb-7c6f768dd6f3"
            }
            this.loadFile();
        },
        //查询文件信息
        loadFile: function(){
            debugger
            var _this = this;
            var token = _this.getQueryString("X-Token");
            var id = _this.getQueryString("fileId");
            $.ajax( {
                    url:http + "/system/file/getById",
                    type:'post',
                    headers:{'X-Token':token},
                    data:{"fileId": id},
                    success:function(resp){
                        var fileInfo = resp.data;
                        _this.config.document.key = fileInfo.fileId + "_" + _this.getRandom();
                        _this.config.document.fileType = fileInfo.fileSuffix.replace(".","");
                        _this.config.document.url = _this.documentUrl + fileInfo.fileId + "&X-Token=" + _this.getQueryString("X-Token");
                        _this.config.document.title = fileInfo.fileName;
                        _this.config.documentType = _this.getDocumentType(fileInfo.fileSuffix.replace(".",""));
                        _this.config.editorConfig.mode = _this.getQueryString("mode");
                        _this.config.editorConfig.callbackUrl = _this.callbackUrl;
                        _this.config.editorConfig.user.id = _this.getQueryString("X-Token");
                        var docEditor = new DocsAPI.DocEditor("placeholder", _this.config);
                    },
                    error:function(data){console.log("error");}
                }
            );

        },
        //获取随机数
        getRandom: function(){
            return parseInt(Math.random() * 10000000000);
        },
        //获取搽查询参数
        getQueryString: function (name) {
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;
        },
        //获取文件类型
        getDocumentType: function (fileType) {
            fileType = fileType.toLowerCase();
            switch (fileType) {
                case "xlsx":
                case "xls":
                case "csv":
                case "ods": return "spreadsheet";
                case "pptx":
                case "ppt":
                case "odp":return "presentation";
                case "docx":
                case "doc":
                case "odt":
                case "pdf":
                case "txt": return "text";
                default: return "text";
            }

        }
    }
    onlyOffice.init();
</script>
</html>